<template>
    <el-dialog width="400px" title="修改密码" :visible.sync="$store.state.dialogFormVisible">
        <el-form :model="changePassForm" :rules="rules" ref="changePassForm">
          <el-form-item label="原密码" label-width="80px" prop="oldPassword">
            <el-input v-model="changePassForm.oldPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" label-width="80px" prop="newPassword">
            <el-input v-model="changePassForm.newPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" label-width="80px" prop="checkPassword">
            <el-input v-model="changePassForm.checkPassword" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleSubmitPassword">提交</el-button>
          <el-button @click="$store.state.dialogFormVisible = false">重置</el-button>
        </div>
      </el-dialog>
</template>

<script>
// 引入校验密码接口
import userModel from '../../api/user.js'
export default {
  data() {
    // 原密码校验
    const validatorOldPass = async (rule, value, callback) => {
      console.log("rule", rule);
      console.log("value", value);
      // 原密码和id
      const pass = {password: value, userId: this.getUserInfo.id}
      const response = await userModel.checkOldPass(pass)
      console.log(response);
      // 如果请求flag为false
      if(!response.flag) {
        callback(new Error('原密码不正确'))
      } else {
        callback()
      }
    }
    // 判断原密码和新密码是否一致
    const validatorChangePass =(rule, value, callback) => {
      if(value == this.changePassForm.newPassword) {
        callback()
      } else {
        callback(new Error('两次输入密码不一致'))
      }
    }
    return {
      rules: {
      // 原密码
      oldPassword: [
        {required: true , message: '原密码不能为空', trigger :'blur'},
        {validator: validatorOldPass, trigger: "blur"}
      ],
      // 新密码
      newPassword: [
        {required: true , message: '新密码不能为空', trigger :'blur'}
      ],
      // 确认密码
      checkPassword: [
        {required: true , message: '确认密码不能为空', trigger :'blur'},
        {validator: validatorChangePass, trigger: "blur"}
      ]
    },
      changePassForm : {
       // 原密码
       oldPassword : "",
       // 新密码
       newPassword : "",
       // 确认密码
       checkPassword : ""
      },
    }
  },
  computed : {
    // 获取用户信息
    getUserInfo(){
      return this.$store.getters.getUserInfo || ""
    }
  },
  methods: {
    // 点击提交
    handleSubmitPassword() {
      this.$refs["changePassForm"].validate(async valid => {
        if (!valid) return
        const submitPassData = {password: this.changePassForm.newPassword, userId: this.getUserInfo.id}
        // 请求修改密码接口
        const response = await userModel.changeUserPass(submitPassData)
        if(response.flag) {
          this.$store.dispatch('handleLogout')
          this.$store.state.dialogFormVisible = false
        }
      })
    },
  },
}
</script>

<style>

</style>
